package com.gulimall.product.controller;

import java.util.Arrays;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gulimall.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.gulimall.product.entity.SpuInfoEntity;
import com.gulimall.product.service.SpuInfoService;

/**
 * spu信息
 *
 * @author ht
 * @email ht035200@163.com
 * @date 2020-04-07 17:20:28
 */
@RestController
@RequestMapping("product/spuinfo")
public class SpuInfoController {
    @Autowired
    private SpuInfoService spuInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(SpuInfoEntity spuInfo, @RequestParam(name = "page", defaultValue = "1") Integer pageNo, @RequestParam(name = "limit", defaultValue = "10") Integer pageSize, @RequestParam(name = "key", required = false) String key) {
        return R.ok().put("page", spuInfoService.queryPage(new Page<>(pageNo, pageSize), spuInfo, key));
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        SpuInfoEntity spuInfo = spuInfoService.getById(id);
        return R.ok().put("spuInfo", spuInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("{id}/up")
    public R up(@PathVariable("id") String id) {
        SpuInfoEntity spuInfo = spuInfoService.getById(id);
        if (spuInfo == null) {
            throw new RuntimeException("当前商品信息不存在");
        }
        spuInfo.setPublishStatus(1);
        spuInfoService.updateById(spuInfo);
        return R.ok();
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody SpuInfoEntity spuInfo) {
        spuInfoService.save(spuInfo);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody SpuInfoEntity spuInfo) {
        spuInfoService.updateById(spuInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        spuInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
